package com.googlecode.dgwt.client.dijit.base.state;

import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.googlecode.dgwt.client.dijit.Dijit;
import com.googlecode.dgwt.client.dijit.Widget;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class HoverState extends EventState {

    private static HoverState instance;
    
    
    public static HoverState get() {
        if (instance == null) {
            instance = new HoverState();
        }
        return instance;
    }
    
    
    private HoverState() {
    }
    
    @Override
    public boolean isActivatedBy(Event event, int eventMask, Widget widget) {
        EventTarget target = event.getRelatedEventTarget();
        return (eventMask & Event.ONMOUSEOVER) != 0 
            && (target == null || !widget.getElement().isOrHasChild(target.<Element>cast()));
    }
    
    @Override
    public boolean isDeactivatedBy(Event event, int eventMask, Widget widget) {
        EventTarget target = event.getRelatedEventTarget();
        return (eventMask & Event.ONMOUSEOUT) != 0 
            && (target == null || !widget.getElement().isOrHasChild(target.<Element>cast()));
    }
    
    @Override
    protected String getDependentStyleClass() {
        return Dijit.getTheme().getHoverName();
    }

    @Override
    public int getEventMask() {
        return Event.ONMOUSEOVER | Event.ONMOUSEOUT;
    }

}
